package com.qzw.demo.service.service.impl;

import com.qzw.demo.api.service.IUserService;
import com.qzw.common.data.BaseMysqlDao;
import com.qzw.demo.api.model.User;
import com.qzw.common.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.qzw.demo.api.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
import com.qzw.demo.service.dao.UserDao;
import com.qzw.demo.service.entity.DmUser;
import java.lang.Byte;
import java.lang.Integer;

@Service
public class UserServiceImpl extends BaseServiceImpl<User, DmUser, Integer, UserQuery> implements IUserService {

	@Autowired
	private UserDao dao;

	@Override protected BaseMysqlDao getDao() {
	return dao;
	}

	@Override
	public User entity2model(DmUser entity) {
		User model = null;
		if(isNotEmpty(entity)){
			model = new User();
			model.setId(entity.getId());
			model.setAge(entity.getAge());
			model.setSex(entity.getSex());
			model.setSysUserId(entity.getSysUserId());
		}
		return model;
	}

	@Override
	public DmUser model2Entity(User model) {
		DmUser entity = null;
		if(isNotEmpty(model)){
			entity = new DmUser();
			entity.setId(model.getId());
			entity.setAge(model.getAge());
			entity.setSex(model.getSex());
			entity.setSysUserId(model.getSysUserId());
		}
		return entity;
	}

	@Override
	public User update(User model) {
		if(model.isNew()){
			model = super.add(model);
		} else {
			Optional<DmUser> entityOptional = dao.findById(model.getId());DmUser entity = entityOptional.get();
			if(isNotEmpty(model.getAge()))
				entity.setAge(model.getAge());
			if(isNotEmpty(model.getSex()))
				entity.setSex(model.getSex());
			if(isNotEmpty(model.getSysUserId()))
				entity.setSysUserId(model.getSysUserId());
			model = entity2model(dao.save(entity));
		}
		return model;
	}


}